home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / newsgrp / group94c.txt / 000040_mslamm@pluto.mscc.huji.ac.il _Wed Dec 28 16:07:53 1994.msg < prev    next >
Internet Message Format  |  1995-02-09  |  3KB

  1. Received: from optima.CS.Arizona.EDU by cheltenham.CS.Arizona.EDU; Wed, 28 Dec 1994 07:07:58 MST
  2. Received: from pluto.mscc.huji.ac.il by optima.CS.Arizona.EDU (5.65c/15) via SMTP
  3.     id AA09657; Wed, 28 Dec 1994 07:07:51 MST
  4. Received: by pluto.mscc.huji.ac.il (AIX 3.2/UCB 5.64/4.03)
  5.           id AA78312; Wed, 28 Dec 1994 16:07:53 +0200
  6. Date: Wed, 28 Dec 1994 16:07:53 +0200 (WET)
  7. From: Zvi Lamm <mslamm@pluto.mscc.huji.ac.il>
  8. To: icon-group@cs.arizona.edu
  9. Subject: Truth-Table generator
  10. Message-Id: <Pine.A32.3.91.941228160447.45969A-100000@pluto.mscc.huji.ac.il>
  11. Mime-Version: 1.0
  12. Content-Type: TEXT/PLAIN; charset=US-ASCII
  13.  
  14.  
  15. I had a need to write a program to output truth tables for boolean
  16. expressions. I used the programmming language J (a sister of APL). The
  17. 'program' was about two lines long.  I wrote a program for the same task
  18. in Icon. I include it below.  This Icon solution is ugly (because of the
  19. programmer, not the language..), but works. I would be happy to hear
  20. comments. Any idea for improvemnet is welcome. 
  21.  
  22. I got alot of help on comp.lang.apl when writing the J version - so don't 
  23. let me down! I think I will write a short note comparing the solutions. 
  24. My idea is to show the way different languages shape your thought. Both J 
  25. and Icon are rather special in that they give the programmer tools not 
  26. found in other languages. Does this interest anyone? 
  27.  
  28. Any way, here goes:
  29.  
  30. #
  31. # Truth-Table genertor Ver -1.0
  32. # 12.1994
  33. #
  34. procedure main()
  35.         n:=2
  36.         m:=2 # number of expressions
  37.  
  38.        # every write(Outl(t:=Truth_Table(n)),"--> ",Expr(t));
  39.  
  40.         # shows many expr to many truth-table lines
  41.  
  42.         every {
  43.                 #write(repl("-",2*n-1+1+4*m))#
  44.                 t:=Truth_Table(n)              & #------------------------#            
  45.                 write(" ")                     & #<-force line break when #
  46.                 writes(Outl(t))                & #  backtracking          #
  47.                 every writes(" | ",Expr(t))      #------------------------#
  48.               }  
  49.  
  50. end
  51.  
  52. procedure Truth_Table(n)
  53. #
  54. # generates lines (as lists) of the truth table for n variables
  55. # works, but is ugly
  56. #
  57.         tabl:=list(n,0)
  58.         every i:=0 to 2^n-1 do                   # all lines
  59.                 {
  60.                     j:=i;                        # convert number to bits
  61.                     k:=n;
  62.                     while (j > 0) do
  63.                         {
  64.                              tabl[k]:=j % 2;
  65.                              k:=k-1;
  66.                              j:=j / 2;
  67.                          }
  68.                     suspend tabl                 # return line
  69.                  }                                
  70.  
  71.  
  72. end
  73.  
  74. procedure Expr(vec)                     
  75. #
  76. # evaluates a list of bits (the logical expr)
  77. # when multiple expr are suspended a mechanism in main prints them side by 
  78. # side
  79. #
  80.         suspend (ior(vec[1],vec[2]) |
  81.         ixor(vec[1],vec[2])) 
  82. end
  83.  
  84. procedure Outl(vec)
  85. #
  86. # prints a list
  87. #
  88.         local s;
  89.         s:=""
  90.         every x:=!vec do s:=s||x||" "
  91.         return s
  92. end
  93.  
  94.